import Toast from '../../static/Vant/toast/toast'

function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function TimerToast(text, time) {
  Toast({
    message: text,
    duration: parseInt(time)
  });
}
export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

function ajax(url, method, data) {
  return new Promise((resolve, reject) => {
    wx.request({
      data,
      method,
      url,
      // url: config.host + url, //开发者服务器接口地址",
      success: res => {
        if (res.data.code === 0) {
          resolve(res.data.data)
        } else {
          reject(res.data)
        }
      }
    });
  })
}

export var request = {
  get: function (url, data) {
    return ajax(url, 'GET', data)
  },
  post: function (url, data) {
    return ajax(url, 'POST', data)
  }
}


export default {
  formatNumber,
  formatTime,
  request,
  TimerToast
}
